<!--
 * @Description: 生态沟渠
-->
<template>
  <BaseMainSlot>
    <template v-slot:zhactbar>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        @add="add"
        @cancel="cancel"
        @edit="edit"
        @del="del"
        @save="save"
      />
    </template>
    <template v-slot:searchbox>
      <div class="label-input mr-20">
        <div class="label">农场名称:</div>
        <div class="input">
          <el-input v-model="ditchName" clearable />
        </div>
      </div>
      <div class="search ml-20">
        <img src="~@/assets/action-bar-icon/search.png" @click="search" />
      </div>
    </template>
    <template v-slot:tablebox>
      <el-table
        v-loading="loading"
        highlight-current-row
        :data="tableData"
        stripe
        border
        height="calc(100% - 50px)"
        @row-click="handleEdit"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="ditchName" label="生态沟渠名称" />
        <el-table-column prop="position" label="位置号" />
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:form>
      <el-form
        ref="formData"
        label-position="left"
        :model="formData"
        class="el-form"
      >
        <el-form-item label="生态沟渠名称:" class="w50p">
          <el-input
            v-model="formData.ditchName"
            :disabled="!canInput"
            clearable
          />
        </el-form-item>
        <el-form-item label="位置:" class="w50p">
          <el-input
            v-model="formData.position"
            :disabled="!canInput"
            clearable
          />
        </el-form-item>
      </el-form>
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import nowpagereq from "@/api/basic-info-manage/ecological-ditch/index";
import BaseMainSlot from "@/components/BaseMainSlot"
import { mymixin } from "@/utils/mixin"
export default {
  name: "EcologicalDitch",
  components: { ActionBar, Pagination, BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      // 沟渠名
      ditchName: "",
      // 选中的数据项数组 plantingUnitId组成 (此数组最多为一个)
      selectedPlantingUnitList: []
    };
  },
  watch: {},
  created() {
    // 获取表格数据
    this.getTableDataList();
  },
  mounted() {},
  methods: {
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        ditchName: this.ditchName
      };
      if (params.ditchName.length > 0) {
        nowpagereq.getList(params).then(res => {
          this.loading = false;
          if (res.code === 1) {
            this.$message.success('查询成功')
          } else {
            this.$message.error('查询失败')
          }
          let data = res.data;
          // 赋值总条数
          this.totalSize = data.length;
          // 赋值数组
          this.tableData = data;
          this.addblankmet()
          this.resetActionBar();
        });
      } else {
        nowpagereq.getList(params).then(res => {
          this.loading = false;
          if (res.code === 0) {
            this.$message.error('查询失败')
          }
          let data = res.data;
          // 赋值总条数
          this.totalSize = res.data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.addblankmet()
          this.resetActionBar();
        });
      }
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    handleEdit(row) {
      if (row.ditchId) {
        this.formData = row;
        this.editActive = true;
        this.saveActive = false;
        this.cancelActive = true;
        this.canInput = false;
      }
    },
    handleSelectionChange(list) {
      this.selectedPlantingUnitList = [];
      let notvalue = true
      this.selectedPlantingUnitList = list.map(item => {
        if (item.ditchId) {
          return item.ditchId
        } else {
          notvalue = false
          return ""
        }
      });
      console.log(this.selectedPlantingUnitList)
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
        return
      }
      if (this.selectedPlantingUnitList.length > 0) this.delActive = true;
      else this.delActive = false;
      // 多个处理为单个数据
      if (this.selectedPlantingUnitList.length > 1) {
        this.$message.warning("员工不可多选删除！");
        this.selectedPlantingUnitList.splice(0, this.selectedPlantingUnitList.length - 1);
        this.delActive = false
      }
    },
    delmessage() {
      nowpagereq.dellist(this.selectedPlantingUnitList).then(
        res => {
          if (res.code === 1) {
            this.$message.success('删除成功')
            this.getTableDataList();
          } else {
            this.$message.error('删除失败')
          }
        }
      )
    },
    save() {
      if (this.nowChanceStau === 'edit') {
        // 编辑接口
        // this.formData.routePointMapperList = this.tagsList;
        nowpagereq.editlist(this.formData).then(res => {
          if (res.code === 1) {
            this.$message.success("修改成功！");
            this.getTableDataList();
          } else {
            this.$message.error("修改失败！");
          }
        });
      } else if (this.nowChanceStau === 'add') {
        nowpagereq.addlist(this.formData).then(res => {
          if (res.code === 1) {
            this.$message.success("新增成功！");
            this.getTableDataList();
          } else {
            this.$message.error("新增失败！");
          }
        });
      }
    }
  }
};
</script>

<style scoped lang="scss">
</style>
